
using System.ComponentModel;
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;

using CIndicators;
using CStock;
using CUtil;

namespace CStock
{
	/// <summary>
	/// Summary description for 
	/// </summary>
	public class DataClassIntraday : DataClass, IDataClassIntraday
    {
        #region private methods, constructor

        protected new bool intraday = true;
        protected DateTime dateIntraday;

	
        public DataClassIntraday(IDataProvider myParent)
		{
            
            this.dateIntraday = DateTime.Now;
            this.parent = myParent;
            this.ResetPathFromConfiguration();
            
            
			
		}
        public DateTime DateIntraday
        {
            get
            {
                return this.dateIntraday;
            }
            set
            {
                this.dateIntraday = value;
                //during deserialization, the parent = null, so do not call it
                if (this.parent != null)
                    ResetPathFromConfiguration();

            }
        }


        public DataClassIntraday():base()
        {
            this.dateIntraday = DateTime.Now;

        }

        #endregion

        #region properties, implementation

        //[System.Xml.Serialization.XmlIgnoreAttribute()]
		[Category("Exchange Data"),Description(@"Data related to the exchange")]
		public override string ID
		{
			get
			{
				return this.iD;
			}
			set
			{
                //should be set only during deserialization!!
				this.iD = value;				
			}
		}
        public override bool ResetPathFromConfiguration()
        {
            //format compatible with serialization
            this.iD = string.Format("{0}_{1:yyyyMMdd}", this.parent.ID, dateIntraday);  

            ProgramContext pc = ProgramContext.Instance;
            Debug.Assert(pc != null, "Initialize ProgramContext first ...");
            if (pc != null)
                this.path = pc.Configuration.Directories.QuotesDataPath + "\\" + this.parent.ID.ToString() + "\\Intraday";
                
            return true;
        }
      
        public void ResetParent(IDataProvider dc)
        {
            this.parent = dc;

        }
        public override bool Serialize()
        {
            this.ResetPathFromConfiguration();
            return base.Serialize();
        }
        //public static bool DeSerializeByDate(ref object o)
        //{
        //    return true;
        //}
        private ArrayList GetIntradayFiles()
        {
            ArrayList files = GetAllAvailableIntradayFiles();
            if (files == null || files.Count <= 0)
            {
                return null;
            }
            return files;
        }
        private void CheckPath()
        {
            Debug.Assert(!String.IsNullOrEmpty(this.iD));
            if (!String.IsNullOrEmpty(this.path))
            {
                ResetPathFromConfiguration();
            }
        }
        public ArrayList GetAllAvailableIntradayFiles()
        {
            //return null;
            //implementation to be changed!!
            //Debug.WriteLine(this.GetType().ToString());

            if (this.GetType() == typeof(DataClassIntraday))
            {
                //Debug.Assert(this.Parent != null, "Parent not set for dataClass " + this.Name);
                //only important for intraday classes !!
                if (this.Parent == null)
                    return null;

                //search for files with the name convention
                //ID + date (without extension)
                //ArrayList intradayColl = new ArrayList();

                CheckPath();

                string mypath = this.path;

                //DirectoryInfo dir = new DirectoryInfo(mypath + "\\" + this.iD + "\\Intraday");
                DirectoryInfo dir = new DirectoryInfo(mypath);

                if (!dir.Exists)
                {
                    dir.Create();
                    return null;
                }
                System.IO.FileInfo[] filesFound = dir.GetFiles();

                System.Collections.ArrayList files = new ArrayList();
                IDataProvider dc = this.Parent as IDataProvider;
                Debug.Assert(dc != null, "SW Error - parent of QuoteCollection Intraday not set");

                string strID = dc.ID;
                for (int i = 0; i < filesFound.GetLength(0); i++)
                {
                    string strFile = filesFound[i].Name;

                    if (strFile.Length > strID.Length)
                    {
                        string strFileID = strFile.Remove(strID.Length, strFile.Length - strID.Length);
                        if (strFileID == strID)
                        {
                            files.Add(filesFound[i]);
                        }
                    }



                }


                return files;
            }
            return null;


        }

#endregion

        #region serialization

        public bool DeserializeCurrentIntraday(ref object o)
        {
            ArrayList files = GetIntradayFiles();
            if (files != null)
            {
                for (int i = 0; i < files.Count; i++)
                {
                    System.IO.FileInfo file = files[i - 1] as System.IO.FileInfo;
                    string str = CUtil.IOUtil.ExtractNameFromFile(file);
                    if(str.Contains(DateTime.Today.ToShortDateString()))
                    {
                         this.iD = str;

                        o = new DataClassIntraday();
                        return base.DeSerialize(ref o);
                    }
                }
                
            }
            return false;

        }
        public override bool DeSerialize(ref object o)
        {
            ArrayList files = GetIntradayFiles();
            if (files != null)
            {
                int i = files.Count;
                System.IO.FileInfo file = files[i - 1] as System.IO.FileInfo;
                //string fileName = CUtil.IOUtil.ExtractNameFromFile(file);
                this.iD = CUtil.IOUtil.ExtractNameFromFile(file);

                //object o = this;
                o = new DataClassIntraday();
                return base.DeSerialize(ref o);
            }

            return false;

        }

        #endregion

    }
}
